/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Distributor.java
 *
 * Created on 23/07/2011, 11:41:12 ص
 */
package com.face;

import com.model.DistributorComboModel;
import com.sql.DistributorSql;
import java.util.ArrayList;
import javax.swing.ImageIcon;

/**
 *
 * @author Khaled Lela
 */
public class Distributor extends javax.swing.JFrame {

    /** Creates new form Distributor */
    public Distributor() {
        initComponents();
        setIconImage(new ImageIcon(getClass().getResource("/images/person.png")).getImage());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        distributorName_TF = new javax.swing.JTextField();
        distributorPhone_TF = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel22 = new javax.swing.JLabel();
        Error_Label = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        newDistributor_Button = new javax.swing.JButton();
        jLabel17 = new javax.swing.JLabel();
        distributorRegion_TF = new javax.swing.JTextField();
        distributorComboBox = new javax.swing.JComboBox();
        clearDistributorTFButton = new javax.swing.JButton();
        distributorQuantity_TF = new javax.swing.JTextField();
        editDistributor_Button = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("التحكم فى بيانات الموزع");
        setResizable(false);

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "بيانات المـــوزع", javax.swing.border.TitledBorder.RIGHT, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Times New Roman", 0, 18))); // NOI18N

        distributorName_TF.setFont(new java.awt.Font("Times New Roman", 0, 18));
        distributorName_TF.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        distributorName_TF.setNextFocusableComponent(distributorRegion_TF);

        distributorPhone_TF.setFont(new java.awt.Font("Times New Roman", 0, 18));
        distributorPhone_TF.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        distributorPhone_TF.setNextFocusableComponent(newDistributor_Button);

        jLabel11.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel11.setText("المنطقة");

        jLabel10.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel10.setText("الكمية");

        jLabel22.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel22.setText("رقم تليفون");

        Error_Label.setFont(new java.awt.Font("Times New Roman", 0, 18));
        Error_Label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        Error_Label.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel3.setText("الموزع");

        newDistributor_Button.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        newDistributor_Button.setText("إضافة موزع");
        newDistributor_Button.setNextFocusableComponent(editDistributor_Button);
        newDistributor_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newDistributor_ButtonActionPerformed(evt);
            }
        });

        jLabel17.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel17.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel17.setText("اسم الموزع");

        distributorRegion_TF.setFont(new java.awt.Font("Times New Roman", 0, 18));
        distributorRegion_TF.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        distributorRegion_TF.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
        distributorRegion_TF.setNextFocusableComponent(distributorPhone_TF);

        distributorComboBox.setFont(new java.awt.Font("Times New Roman", 0, 18));
        distributorComboBox.setModel(new DistributorComboModel());
        distributorComboBox.setNextFocusableComponent(distributorName_TF);
        distributorComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                distributorComboBoxActionPerformed(evt);
            }
        });

        clearDistributorTFButton.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        clearDistributorTFButton.setText("مسح");
        clearDistributorTFButton.setNextFocusableComponent(distributorComboBox);
        clearDistributorTFButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearDistributorTFButtonActionPerformed(evt);
            }
        });

        distributorQuantity_TF.setEditable(false);
        distributorQuantity_TF.setFont(new java.awt.Font("Times New Roman", 0, 18));
        distributorQuantity_TF.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        editDistributor_Button.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        editDistributor_Button.setText("تعديل موزع");
        editDistributor_Button.setNextFocusableComponent(clearDistributorTFButton);
        editDistributor_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editDistributor_ButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                                .addComponent(clearDistributorTFButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 2, Short.MAX_VALUE)
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel5Layout.createSequentialGroup()
                                        .addGap(136, 136, 136)
                                        .addComponent(editDistributor_Button)
                                        .addGap(120, 120, 120)
                                        .addComponent(newDistributor_Button))
                                    .addGroup(jPanel5Layout.createSequentialGroup()
                                        .addGap(85, 85, 85)
                                        .addComponent(Error_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(distributorQuantity_TF)
                                    .addComponent(distributorRegion_TF, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel11)
                                    .addComponent(jLabel10))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(distributorPhone_TF)
                                    .addComponent(distributorName_TF, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE))
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel5Layout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel17))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel22)))
                                .addGap(2, 2, 2))))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGap(86, 86, 86)
                        .addComponent(distributorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(distributorComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(29, 29, 29)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel17)
                            .addComponent(distributorName_TF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(28, 28, 28)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(distributorPhone_TF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel22)))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(distributorRegion_TF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28)
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(distributorQuantity_TF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel10)))
                    .addComponent(jLabel11))
                .addGap(28, 28, 28)
                .addComponent(Error_Label, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newDistributor_Button)
                    .addComponent(clearDistributorTFButton)
                    .addComponent(editDistributor_Button))
                .addContainerGap(25, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 547, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 327, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void editDistributor_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editDistributor_ButtonActionPerformed
        String oldName = (String) distributorComboBox.getSelectedItem();

        //if there is no value on supplierName TF escap , show error and reset all suppliers TF
        if (!distributorName_TF.getText().equals("")) {
            name = distributorName_TF.getText();
            region = distributorRegion_TF.getText();
            try {
                phone = Integer.valueOf(distributorPhone_TF.getText());
            } catch (Exception e) {
                phone = 0;
            }
            boolean getError = new DistributorSql().updateDistributorRow(oldName, name, region, phone);
            //if No error while adding supplier Information on DB add supplierName to supplierNameComboBox
            if (!getError) {
                distributorComboBox.setModel(new DistributorComboModel());
                clearTF();
            } else {
                Error_Label.setText(" الموزع  موجود");
            }
            //show error
        } else {
            Error_Label.setText("يجب إدخال اسم موزع");
        }
}//GEN-LAST:event_editDistributor_ButtonActionPerformed

    private void clearDistributorTFButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearDistributorTFButtonActionPerformed
        // Clear all s TF process Complete
        clearTF();
}//GEN-LAST:event_clearDistributorTFButtonActionPerformed

    private void distributorComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_distributorComboBoxActionPerformed
        name = (String) distributorComboBox.getSelectedItem();
        if (!name.equals("")) {
            ArrayList<String> distributorRow = new ArrayList<String>();
            distributorRow = new DistributorSql().getDistributorRow(name);
            distributorName_TF.setText(distributorRow.get(0));
            distributorRegion_TF.setText(distributorRow.get(1));
            distributorPhone_TF.setText(distributorRow.get(2));
            distributorQuantity_TF.setText(distributorRow.get(3));
        } else {
            clearTF();
        }
}//GEN-LAST:event_distributorComboBoxActionPerformed

    private void newDistributor_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newDistributor_ButtonActionPerformed
        //if there is no value on supplierName TF escap , show error and reset all suppliers TF
        if (!distributorName_TF.getText().equals("")) {
            name = distributorName_TF.getText();
            region = distributorRegion_TF.getText();
            try {
                phone = Integer.valueOf(distributorPhone_TF.getText());
            } catch (Exception e) {
                phone = 0;
            }
            boolean getError = new DistributorSql().setDistributorRow(name, region, phone);
            //if No error while adding supplier Information on DB add supplierName to supplierNameComboBox
            if (!getError) {
                distributorComboBox.addItem(name);
                clearTF();
                //if error exist show error
            } else {
                Error_Label.setText("الموزع موجود");
            }
            //show error
        } else {
            Error_Label.setText("يجب إدخال اسم موزع");
        }
}//GEN-LAST:event_newDistributor_ButtonActionPerformed
    private void clearTF() {
        distributorName_TF.setText("");
        distributorRegion_TF.setText("");
        distributorPhone_TF.setText("");
        distributorQuantity_TF.setText("");

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Error_Label;
    private javax.swing.JButton clearDistributorTFButton;
    private javax.swing.JComboBox distributorComboBox;
    private javax.swing.JTextField distributorName_TF;
    private javax.swing.JTextField distributorPhone_TF;
    private javax.swing.JTextField distributorQuantity_TF;
    private javax.swing.JTextField distributorRegion_TF;
    private javax.swing.JButton editDistributor_Button;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JButton newDistributor_Button;
    // End of variables declaration//GEN-END:variables
    String name;
    String region;
    int phone;
    int quantity;
}
